/*
 * Created by JFormDesigner on Tue Jul 02 10:38:01 CST 2024
 */

package scct.zhang.ui;

import scct.zhang.dao.GroupDao;
import scct.zhang.dao.UserDao;
import scct.zhang.model.User;
import scct.zhang.utils.Tools;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;

/**
 * @author lenovo
 */
public class DlgGroupAdd extends JDialog {
    public DlgGroupAdd(Window owner) {
        super(owner);
        initComponents();
        initView();
    }

    private void initView() {
        UserDao userDao = new UserDao();
        List<User> users = userDao.getAllUser();
        for (User user:users){
            creatornameBox.addItem(user.getUsername());
        }
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        String groupname=txtgroup.getText();
        String creatorname= (String) creatornameBox.getSelectedItem();
        String createtime= Tools.getCreatetime();

        if (groupname.isEmpty() || !groupname.matches("[a-zA-Z0-9_\\u4e00-\\u9fa5]{1,10}$")){
            JOptionPane.showMessageDialog(this,"请输入合法群组名");
            return;
        }
        GroupDao groupDao=new GroupDao();
        if (groupDao.addGroup(groupname,creatorname,createtime)){
            JOptionPane.showMessageDialog(this,"添加成功");
        }

        this.dispose();
    }

    private void btnCancle(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        label1 = new JLabel();
        label2 = new JLabel();
        btnAdd = new JButton();
        btnCancle = new JButton();
        creatornameBox = new JComboBox();
        txtgroup = new JTextField();

        //======== this ========
        setTitle("\u6dfb\u52a0\u7fa4\u7ec4");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7fa4\u7ec4\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(95, 50), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u521b\u5efa\u8005");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(95, 105), label2.getPreferredSize()));

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(e -> btnAdd(e));
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(90, 170), btnAdd.getPreferredSize()));

        //---- btnCancle ----
        btnCancle.setText("\u53d6\u6d88");
        btnCancle.addActionListener(e -> btnCancle(e));
        contentPane.add(btnCancle);
        btnCancle.setBounds(new Rectangle(new Point(220, 170), btnCancle.getPreferredSize()));
        contentPane.add(creatornameBox);
        creatornameBox.setBounds(180, 95, 145, creatornameBox.getPreferredSize().height);
        contentPane.add(txtgroup);
        txtgroup.setBounds(180, 50, 140, txtgroup.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JLabel label1;
    private JLabel label2;
    private JButton btnAdd;
    private JButton btnCancle;
    private JComboBox creatornameBox;
    private JTextField txtgroup;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
